﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using NerdDinner.Models;

namespace NerdDinner.Controllers
{
    public class JsonDinner
    {
        public int DinnerID { get; set; }
        public DateTime EventDate { get; set; }
        public string Title { get; set; }
        public double Latitude { get; set; }
        public double Longitude { get; set; }
        public string Description { get; set; }
        public int RSVPCount { get; set; }
        public string Url { get; set; }
    }

    public class SearchController : Controller
    {
        private readonly DinnerRepository dinnerRepository = new DinnerRepository();
        //
        // AJAX: /Search/SearchByLocation
        [HttpPost]
        public ActionResult SearchByLocation(float longitude, float latitude)
        {
            IQueryable<Dinner> dinners = dinnerRepository.FindByLocation(latitude, longitude);
            IQueryable<JsonDinner> jsonDinners = from dinner in dinners
                                                 select new JsonDinner
                                                            {
                                                                DinnerID = dinner.DinnerID,
                                                                Latitude = dinner.Latitude,
                                                                Longitude = dinner.Longitude,
                                                                Title = dinner.Title,
                                                                Description = dinner.Description,
                                                                RSVPCount = dinner.RSVPs.Count
                                                            };
            List<JsonDinner> data = jsonDinners.ToList();
            return Json(data);
        }
    }
}